﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects.DataClasses;
namespace ZhaoPin.Business
{
    public class ExamRoomArrangement
    {
        protected string[] typeA = { "呼吸内科医师", "内分泌科医师" };
        protected string[] typeB = { "感染管理", "医政管理", "计算机中心" };
        RecruitContainer rc = new RecruitContainer();
        protected Dictionary<string, string[]> dict = new Dictionary<string, string[]>();
        float ratio = 0.79f;
        public ExamRoomArrangement()
        {
            dict.Add("医疗", typeA);
            dict.Add("其他", typeB);
        }
        public void ArrangeExamRoom()
        {
            IQueryable<ExamRoom> ers = from er1 in rc.ExamRooms orderby er1.Number select er1;
            Random rnd = new Random();
            foreach (string tk in dict.Keys)
            {
                IQueryable<Person> ps = from p1 in rc.People where dict[tk].Contains(p1.TDictPost.FName) select p1;
                List<Person> psl = ps.ToList();
                int roomIndex = 0;
                do
                {
                    ExamRoom room = ers.ElementAt(roomIndex);
                    if ((float)psl.Count / (float)room.Capacity > ratio)
                    {
                        for (int j = 0; j < room.Capacity; j++)
                        {
                            int temp = rnd.Next(psl.Count);
                            Person person = psl.ElementAt(temp);
                            Person tempP = ps.Single(p => p.Id.Equals(person.Id));
                            tempP.ExamRoomId = room.Id;
                            tempP.ExamSeatNumber = j + 1;
                            tempP.IsArranged = true;
                            psl.RemoveAt(temp);
                        }
                    }
                    roomIndex++;
                } while (psl.Count > 0);
            }
            rc.SaveChanges();
        }
        protected IQueryable<Person> GetPeopleInGroup(string[] type)
        {
            IQueryable<Person> result = from p in rc.People where type.Contains(p.TDictPost.FName) select p;
            return result;
        }
    }
}